<template>
  <div
    v-loading="loading"
    class="login-container"
    element-loading-text="正在登录,请稍候..."
    element-loading-spinner="el-icon-loading"
    element-loading-background="#2d3a4b"
  >
  </div>
</template>

<script>
  import { getToken } from "@/utils/auth/token"; // get token from cookie
  import { redirectUriToReturnUrl } from "@/utils/helpers/wx-helper";

  export default {
    name: "AuthRedirect",
    data() {
      return {
        loading: true,
        returnUrl: undefined
      };
    },
    created() {
      const returnUrl = this.$route.query && this.$route.query.redirect;
      this.returnUrl = redirectUriToReturnUrl(returnUrl);
      var token = getToken();
      if (token) {
        location.href = this.returnUrl || "/";
      }
      const code = this.$route.query && this.$route.query.code;
      const state = this.$route.query && this.$route.query.state;
      this.wxUserInfoCallback(code, state);
    },
    methods: {
      wxUserInfoCallback(code, state) {
        this.$store
          .dispatch("account/weixinComponentLogin", { code, state })
          .then(result => {
              location.href = this.returnUrl || "/";
          })
          .catch(err => {
            console.log("err", err);
          });
      }
    }
  };
</script>
<style lang="scss" scoped>

  .login-container {
    height: 100%;
    width: 100%;
    overflow: hidden;

  }
</style>
